package com.liushuang.waiwaiim.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.liushuang.waiwaiim.model.bean.GroupInfo;
import com.liushuang.waiwaiim.model.bean.InvationInfo;
import com.liushuang.waiwaiim.model.bean.UserInfo;
import com.liushuang.waiwaiim.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class InviteTableDao {

    private DBHelper mDBHelper;

    public InviteTableDao(DBHelper dbHelper){
        mDBHelper = dbHelper;
    }

    //添加邀请信息
    public void addInvitation(InvationInfo invationInfo){

        SQLiteDatabase db = mDBHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_REASON, invationInfo.getReason());
        values.put(InviteTable.COL_STATUS, invationInfo.getStatus().ordinal());

        UserInfo userInfo = invationInfo.getUser();

        if (userInfo != null){//邀请信息由用户发起
            values.put(InviteTable.COL_USER_HXID, userInfo.getHxid());
            values.put(InviteTable.COL_USER_NAME, userInfo.getName());
        }else {//邀请加入群组
            values.put(InviteTable.COL_GROUP_HXID, invationInfo.getGroup().getGroupId());
            values.put(InviteTable.COL_GROUP_NAME, invationInfo.getGroup().getGroupName());
            //table_invite表中主键是user_hxid，所以不能为空，当为群组邀请时,将邀请人设置为user_hxid
            values.put(InviteTable.COL_USER_HXID, invationInfo.getGroup().getInvatePerson());
        }

        db.replace(InviteTable.TABLE_NAME, null, values);
        db.close();
    }

    //获取所有邀请信息
    public List<InvationInfo> getInvitations(){

        SQLiteDatabase db = mDBHelper.getReadableDatabase();

        String sql = "select * from " + InviteTable.TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);

        List<InvationInfo> invationInfos = new ArrayList<>();

        if (cursor != null){
            while (cursor.moveToNext()){
                InvationInfo invationInfo = new InvationInfo();

                invationInfo.setReason(cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON)));
                invationInfo.setStatus(int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS))));

                String groupId = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID));

                if (groupId == null){
                    UserInfo userInfo = new UserInfo();

                    userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                    userInfo.setName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                    userInfo.setNick(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));

                    invationInfo.setUser(userInfo);
                }else{
                    GroupInfo groupInfo = new GroupInfo();

                    groupInfo.setGroupId(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                    groupInfo.setGroupName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)));
                    groupInfo.setInvatePerson(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));

                    invationInfo.setGroup(groupInfo);
                }

                invationInfos.add(invationInfo);
            }
        }

        cursor.close();
        db.close();

        return invationInfos;
    }

    // 将int类型状态转换为邀请的状态
    private InvationInfo.InvitationStatus int2InviteStatus(int intStatus) {
        if (intStatus == InvationInfo.InvitationStatus.NEW_INVITE.ordinal()) {
            return InvationInfo.InvitationStatus.NEW_INVITE;
        }

        if (intStatus == InvationInfo.InvitationStatus.INVITE_ACCEPT.ordinal()) {
            return InvationInfo.InvitationStatus.INVITE_ACCEPT;
        }

        if (intStatus == InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER.ordinal()) {
            return InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER;
        }

        if (intStatus == InvationInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal()) {
            return InvationInfo.InvitationStatus.NEW_GROUP_INVITE;
        }

        if (intStatus == InvationInfo.InvitationStatus.NEW_GROUP_APPLICATION.ordinal()) {
            return InvationInfo.InvitationStatus.NEW_GROUP_APPLICATION;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_INVITE_DECLINED;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_ACCEPT_INVITE;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_REJECT_APPLICATION;
        }

        if (intStatus == InvationInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal()) {
            return InvationInfo.InvitationStatus.GROUP_REJECT_INVITE;
        }

        return null;
    }

    //删除邀请
    public void removeInvitation(String hxId){
        if (hxId == null){
            return;
        }

        SQLiteDatabase db = mDBHelper.getWritableDatabase();

        db.delete(InviteTable.TABLE_NAME, InviteTable.COL_USER_HXID + "=?", new String[]{hxId});

        db.close();
    }

    //更新邀请状态
    public void updateInvitationStatus(InvationInfo.InvitationStatus invitationStatus, String hxId) {

        if (hxId == null){
            return;
        }

        SQLiteDatabase db = mDBHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_STATUS, invitationStatus.ordinal());

        db.update(InviteTable.TABLE_NAME, values, InviteTable.COL_USER_HXID + "=?", new String[]{hxId});
    }
}
